﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;


namespace Restaurant
{
    /// <summary>
    /// Interaction logic for Create.xaml
    /// </summary>
    public partial class CreateWindow : Window
    {
        public CreateWindow()
        {
            InitializeComponent();
        }

        private void ComboBox_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
        {
            //var workerItems = new List<string>();
            //workerItems.Add("Chief");
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            if (IDw.Text != "" && NameW.Text!="" && comboBox.SelectedIndex>=0)
            {
                string position = ((ComboBoxItem)comboBox.SelectedValue).Content.ToString();
                IWorker worker = null;
                switch (position)
                {
                    case "Chef":
                        worker = new Chef(int.Parse(IDw.Text), NameW.Text, position);
                        break;
                    case "Waiter":
                        worker = new Waiter(int.Parse(IDw.Text), NameW.Text, position);
                        break;
                    case "Server":
                        worker = new Server(int.Parse(IDw.Text), NameW.Text, position);
                        break;
                }

                StringBuilder dataWorkerBuilder = new StringBuilder();
                StreamWriter writer = new StreamWriter("../../workers.txt", true);
                dataWorkerBuilder.Append(worker.ID.ToString());
                dataWorkerBuilder.Append("|");
                dataWorkerBuilder.Append(worker.Name);
                dataWorkerBuilder.Append("|");
                dataWorkerBuilder.Append(worker.OfficePosition);
                dataWorkerBuilder.Append("|");
                writer.WriteLine(dataWorkerBuilder);
                writer.Close();
                MessageBox.Show("User is created");
                this.Close();
            }
            else 
            {
                if (IDw.Text == "")
                {
                    MessageBox.Show("Please, generate ID");
                }
                else if (NameW.Text == "")
                {
                    MessageBox.Show("Please, input name");
                }
                else if (comboBox.SelectedIndex < 0)
                {
                    MessageBox.Show("Please, choose position");
                }
            }
        }

        //private string GetMaxID()
        //{
        //    string maxNumber="1";
        //    StringBuilder workersList = FileReader.ReadFile("../../workers.txt", "error.log");
        //    string[] workers = workersList.ToString().Split(new char[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);
        //    foreach (var worker in workers)
        //    {
        //        string[] token = worker.Split(new char[] { '|' }, StringSplitOptions.RemoveEmptyEntries);
        //        string num =  token[0].Trim();
        //    }
        //    return maxNumber;
        //}

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            Random random = new Random();
            int randomNumber = random.Next(100000, 999999);
            IDw.Text = randomNumber.ToString();
        }
    }
}
